function [NEWEEG ChannelInfo] = MergeEEGDATAByRegion(subjectinfo, OLDEEG)

%Load the data (For debugging..)
% subjectinfo = SubjectInfo.Load('Subject DS');
% OLDEEG = pop_loadset('filename', subjectinfo.Datasets(1).PreprocessedFilename);
ChannelInfo = [];
OLDEEG = eeg_checkset(OLDEEG);

%% Get the assignments per channel
% Make a list of predefined regions to search for..
chandefs = [];
chandefs(end+1).Name = 'FROA';    chandefs(end).SourceType = 'RegionAssignment';   chandefs(end).Source = {'FRO-A'};
chandefs(end+1).Name = 'FROP';    chandefs(end).SourceType = 'RegionAssignment';   chandefs(end).Source = {'FRO-P'};
chandefs(end+1).Name = 'PARA';    chandefs(end).SourceType = 'RegionAssignment';   chandefs(end).Source = {'PAR-A'};
chandefs(end+1).Name = 'PARO';    chandefs(end).SourceType = 'RegionAssignment';   chandefs(end).Source = {'PAR-O'};
chandefs(end+1).Name = 'IPSS';    chandefs(end).SourceType = 'RegionAssignment';   chandefs(end).Source = {'IPS-S'};
chandefs(end+1).Name = 'IPSI';    chandefs(end).SourceType = 'RegionAssignment';   chandefs(end).Source = {'IPS-I'};
chandefs(end+1).Name = 'TEMS';    chandefs(end).SourceType = 'RegionAssignment';   chandefs(end).Source = {'TEM-S'};
chandefs(end+1).Name = 'TEMM';    chandefs(end).SourceType = 'RegionAssignment';   chandefs(end).Source = {'TEM-M'};
chandefs(end+1).Name = 'TEMI';    chandefs(end).SourceType = 'RegionAssignment';   chandefs(end).Source = {'TEM-I'};
chandefs(end+1).Name = 'OCCD';    chandefs(end).SourceType = 'RegionAssignment';   chandefs(end).Source = {'OCC-D'};
chandefs(end+1).Name = 'OCCV';    chandefs(end).SourceType = 'RegionAssignment';   chandefs(end).Source = {'OCC-V'};
% Add the joined regions
chandefs(end+1).Name = 'TEM';    chandefs(end).SourceType = 'RegionAssignment';   chandefs(end).Source = {'TEM-S', 'TEM-M', 'TEM-I'};
chandefs(end+1).Name = 'OCC';    chandefs(end).SourceType = 'RegionAssignment';   chandefs(end).Source = {'OCC-D', 'OCC-V'};
chandefs(end+1).Name = 'IPS';    chandefs(end).SourceType = 'RegionAssignment';   chandefs(end).Source = {'IPS-S', 'IPS-I'};
% Add all manually selected sets
roiFn = fieldnames(subjectinfo.RoiSelections);
for msI = 1:length(roiFn)
    chandefs(end+1).Name = roiFn{msI};
    chandefs(end).SourceType = 'Electrodes';
    chandefs(end).Source = subjectinfo.RoiSelections.(roiFn{msI});
end
% Display a resulting checklist
for chandefsI = 1:length(chandefs), cTemp.(chandefs(chandefsI).Name) = chandefs(chandefsI).Source; end
disp('Regions of interest:'); disp(cTemp);

%% Map the electrodes and merge..
eegdata = zeros(length(chandefs), size(OLDEEG.data, 2));
for r = 1:length(chandefs)
    disp(['New channel ' chandefs(r).Name '...']);
    Elecs = [];
    switch chandefs(r).SourceType        
        case 'RegionAssignment'
            for cdsI = 1:length(chandefs(r).Source)
                Elecs = [Elecs ElectrodeInfo.FindByRegion(subjectinfo.Electrodes, chandefs(r).Source{cdsI})];
            end
        case 'Electrodes'
            for cdsI = 1:length(chandefs(r).Source)
                Elecs = [Elecs ElectrodeInfo.FindByLabel(subjectinfo.Electrodes, chandefs(r).Source{cdsI})];
            end
    end
    ChannelInfo(r).Name = chandefs(r).Name;
    ChannelInfo(r).ElectrodeCount = length(Elecs);
    ChannelInfo(r).Electrodes = Elecs;
    if ~isempty(Elecs)
        Labels = {Elecs.Label};
        disp(Labels);
        tf = false(1, size(OLDEEG.data, 1));
        for l = 1:length(Labels)
            tf = tf | strcmp({OLDEEG.chanlocs.labels}, Labels{l});
        end
        eegdata(r,:) = mean(OLDEEG.data(tf,:),1);
    end
end
clearvars -except subjectinfo OLDEEG eegdata ChannelInfo chandefs

%% Create new EEG structure
assignin('base', 'eegdata_import_mergedregions', eegdata);
NEWEEG = pop_importdata( 'dataformat', 'array', 'data', 'eegdata_import_mergedregions', ...
    'setname', 'RegionsMerged', ...
    'subject', ['Reg' subjectinfo.PrimaryPrefix], ...
    'condition', 'Sphere', ...
    'srate', 512, 'pnts', size(eegdata,2), 'xmin', 0, ...
    'nbchan', size(eegdata,1), 'ref', 'avg');
evalin('base', 'clear eegdata_import_mergedregions');
NEWEEG = eeg_checkset( NEWEEG );

%% Load labels and locations into EEG set
presetXyz.FROA.x    = -0.30; presetXyz.FROA.y    =  0.15; presetXyz.FROA.z    =  0.40;
presetXyz.FROP.x    = -0.05; presetXyz.FROP.y    = -0.07; presetXyz.FROP.z    =  0.50;
presetXyz.PARA.x    = -0.40; presetXyz.PARA.y    = -0.10; presetXyz.PARA.z    =  0.35;
presetXyz.PARO.x    = -0.15; presetXyz.PARO.y    = -0.40; presetXyz.PARO.z    =  0.10;
presetXyz.IPSS.x    = -0.10; presetXyz.IPSS.y    = -0.20; presetXyz.IPSS.z    =  0.34;
presetXyz.IPSI.x    = -0.30; presetXyz.IPSI.y    = -0.17; presetXyz.IPSI.z    =  0.15;
presetXyz.IPS.x     = -0.20; presetXyz.IPS.y     = -0.18; presetXyz.IPS.z     =  0.25;
presetXyz.TEMS.x    = -0.75; presetXyz.TEMS.y    = -0.15; presetXyz.TEMS.z    = -0.20;
presetXyz.TEMM.x    = -0.80; presetXyz.TEMM.y    = -0.20; presetXyz.TEMM.z    = -0.35;
presetXyz.TEMI.x    = -0.85; presetXyz.TEMI.y    = -0.25; presetXyz.TEMI.z    = -0.45;
presetXyz.OCCD.x    = -0.30; presetXyz.OCCD.y    = -0.50; presetXyz.OCCD.z    = -0.10;
presetXyz.OCCV.x    = -0.40; presetXyz.OCCV.y    = -0.60; presetXyz.OCCV.z    = -0.30;
presetXyz.TEM.x     = -0.80; presetXyz.TEM.y     = -0.20; presetXyz.TEM.z     = -0.33;
presetXyz.OCC.x     = -0.35; presetXyz.OCC.y     = -0.50; presetXyz.OCC.z     = -0.20;
presetXyz.m1OCCV.x  = -0.40; presetXyz.m1OCCV.y  = -0.60; presetXyz.m1OCCV.z  = -0.10;
presetXyz.m1OCCD.x  = -0.30; presetXyz.m1OCCD.y  = -0.50; presetXyz.m1OCCD.z  = -0.10;
presetXyz.m1TEM.x   = -0.80; presetXyz.m1TEM.y   = -0.20; presetXyz.m1TEM.z   = -0.33;
presetXyz.m1IPSS.x  = -0.10; presetXyz.m1IPSS.y  = -0.20; presetXyz.m1IPSS.z  =  0.34;
presetXyz.m1Motor.x =  0.00; presetXyz.m1Motor.y =  0.00; presetXyz.m1Motor.z =  0.00;
fid = fopen('Regions.xyz', 'wt');
for r = 1:length(chandefs)
    fprintf(fid, '%i \t%3.2f \t%3.2f \t%3.2f \t%s\n', r, ...
        presetXyz.(chandefs(r).Name).x, ...
        presetXyz.(chandefs(r).Name).y, ...
        presetXyz.(chandefs(r).Name).z, ...
        chandefs(r).Name);
end
fclose(fid);
NEWEEG = pop_chanedit(NEWEEG,  'load', {'Regions.xyz', 'filetype', 'xyz'});
NEWEEG = eeg_checkset(NEWEEG);

%% Transfer markers
NEWEEG.event = OLDEEG.event;
NEWEEG.urevent = OLDEEG.urevent;
NEWEEG = eeg_checkset(NEWEEG);

% pop_saveset(NEWEEG, 'filename', 'NEWEEG.set');
end